package com.lmc.edrive;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lmc.edrive.database.DbManager;

/**
 * Servlet implementation class Alert
 */
@WebServlet("/Alert")
public class Alert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Alert() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName=request.getParameter("username");
		String lat=request.getParameter("lat");
		String longt=request.getParameter("long");
		double lat1=0;
		double long1=0;
		if(lat!=null&&longt!=null){
			lat1=Double.parseDouble(lat);
			long1=Double.parseDouble(longt);
			getAlert(userName, lat1, long1, response);
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	public void getAlert(String emailId,double lat1,double long1,HttpServletResponse response) throws IOException{
		PrintWriter pw=response.getWriter();
		DbManager db=DbManager.getInstance();
		if (emailId!=null && db.isUserExist(emailId)) {
			List<com.lmc.edrive.data.AlertData> alerts =db.getAlert(lat1,long1);
			// convert java object to JSON format,
			// and returned as JSON formatted string
			Gson gson = new Gson();
			String json = gson.toJson(alerts);
			pw.println(json);
			
		}else {
			pw.println("User Doesn't Exist");
		}
	}

}
